package com.shunhe.storage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.ruitu.arad.Arad;
import com.ruitu.arad.util.AppUtils;
import com.ruitu.arad.util.ToastUtils;
import com.shunhe.storage.support.util.ManifestUtil;
import com.shunhe.storage.ui.system.HomeActivity;

public class MainActivity extends AppCompatActivity {
    private TextView tv_ver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // 兼容使用安卓器安装程序后,直接点击打开出现的各种异常
        if (!this.isTaskRoot()) { //判断该Activity是不是任务空间的源Activity，“非”也就是说是被系统重新实例化出来
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;// finish()之后该活动会继续执行后面的代码，你可以logCat验证，加return避免可能的exception
            }
        }

        setContentView(R.layout.activity_main);
        tv_ver = findViewById(R.id.tv_ver);

        tv_ver.setText("v" + AppUtils.getAppVersionName());

        ManifestUtil.getAppMetaDataString(this, "my_key", "000");

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isDestroy) {
                    int currVersion = AppUtils.getAppVersionCode();
                    if (Arad.preferences.getInteger("last_version", 0) < currVersion) {
                        //说明以前安装的版本没有当前的版本高,说明是刚安装的新版本
                        startGuidePage();
                        Arad.preferences.putInteger("last_version", currVersion).flush();//已经启动过引导页面
                    } else {
                        startHomePage();
                    }
                }
                finish();
            }
        }, 1000);
    }

    // 启动主页面
    private void startHomePage() {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    }

    // 启动引导页面
    private void startGuidePage() {
        ToastUtils.showLongSafe("启动引导页面");
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    }

    private boolean isDestroy = false;//页面是否已经销毁

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestroy = true;
    }
}